﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Organization;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.Security;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Areas.Organization.Controllers
{
    public class InSideController : BaseController
    {
        public IInSideModelContract InSideModelContract { get; set; }
        public IDepartmentContract DepartmentContract { get; set; }
        public IStaffContract StaffContract { get; set; }
        #region 考勤列表
        // GET: Organization/InSide
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult IndexAsync(string Staff, DateTime Month)
        {
            var query = from ism in InSideModelContract.Entities
                        join s in StaffContract.Entities
                        on ism.CreatorId equals s.Id.ToString()
                        where ism.IsDeleted == false && s.IsDeleted == false && ism.CreatedTime.Year == (Month.Year) && ism.CreatedTime.Month == (Month.Month)
                        select new
                        {
                            //人员ID
                            StaffId = s.Id,
                            //考勤ID
                            InSideId = ism.Id,
                            //人员名称
                            Name = s.Name,
                            //上班时间
                            StartTime = ism.StartTime,
                            //上班打卡描述--外出，正常，请假
                            StartContent = ism.StartContent,
                            //上班考勤状态 1：正常 ，2：迟到
                            StartLeaveType = ism.StartLeaveType,
                            //上班打卡地址
                            StartLocation = ism.StartLocation,
                            //上班打卡图片
                            StartImagePage = ism.StartImagePage,
                            //下班时间
                            EndTime = ism.EndTime,
                            //下班打卡描述 --外出，正常，请假
                            EndContent = ism.EndContent,
                            //下班考勤状态 1：正常，3：早退
                            EndLeaveType = ism.EndLeaveType,
                            //下班打卡地址
                            EndLocation = ism.EndLocation,
                            //下班打卡图片
                            EndImagePage = ism.EndImagePage,
                            //上班经度
                            StartLongitude = ism.StartLongitude,
                            //上班纬度
                            StartLatitude = ism.StartLatitude,
                            //下班经度
                            EndLongitude = ism.EndLongitude,
                            //下班纬度
                            EndLatitude = ism.EndLatitude,
                            //关联人员ID
                            CreatorId = ism.CreatorId,
                            //打卡时间
                            CreatedTime = ism.CreatedTime

                        };

            if (Staff != "")
            {
                query = query.Where(m => m.CreatorId == Staff);
            }

            var total = query.Count();

            //此处可以采用匿名对象 GridData<object>
            GridData<object> gridData = new GridData<object>() { Total = total, Rows = query.ToList() };

            //此处采用重写后的jsonResult
            return JsonEx(gridData, JsonRequestBehavior.AllowGet, "yyyy-MM-dd HH:mm:ss");
        }
        #endregion

        #region 上下班考勤图片 
        //上下班考勤图片 
        [Description("内勤-上班考勤图片")]
        public ActionResult SratImage(int id, int count)
        {
            if (count == 1)
            {
                var entity = InSideModelContract.GetByKey(id);
                //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
                if (entity == null)
                {
                    throw new Exception("未找到该实体");
                }
                var imgurl = "/Images/SideImage/" + entity.StartImagePage;
                ViewData["imgurl"] = imgurl;
            }
            if (count == 2)
            {
                var entity = InSideModelContract.GetByKey(id);
                //后台容错，有异常数据直接抛出。框架会自动跳转到错误页面。
                if (entity == null)
                {
                    throw new Exception("未找到该实体");
                }
                var imgurl = "/Images/SideImage/" + entity.EndImagePage;
                ViewData["imgurl"] = imgurl;
            }

            return View();
        } 
        #endregion
         
        #region 下拉框
        /// <summary>  
        /// 部门
        /// </summary>  
        [AjaxOnly]
        public ActionResult GetDepartmentlist()
        {
            //获取省份数据   
            var queryResult = DepartmentContract.Entities;
            return JsonEx(queryResult.ToList(), JsonRequestBehavior.AllowGet);
        }
        /// <summary>  
        /// 获取城市  
        /// </summary>  
        /// <param name="did"></param>  
        /// <returns></returns>  
        [AjaxOnly]
        public ActionResult GetStafflist(int did)//根据省份ID获取城市  
        {
            //var id = (did == "") ? 0 : Convert.ToInt32(did.Trim());
            //did = did.Trim();
            var list = StaffContract.Entities.Where(m => m.DepartmentID.Equals(did)).ToList();//城市ID对应值  
            return JsonEx(list, JsonRequestBehavior.AllowGet);
        }
        #endregion 
         
    }
}